package com.funtonow.jqdk.utils;

import android.content.Context;
import android.content.Intent;
import android.os.Build;

import com.funtonow.jqdk.base.Constants;
import com.funtonow.jqdk.base.SqjAppliaction;
import com.funtonow.jqdk.bean.GameThirdInfoListBean;
import com.funtonow.jqdk.bean.UserInfoBean;
import com.funtonow.jqdk.ui.WebViewActivity;
import com.umeng.analytics.MobclickAgent;

import java.io.IOException;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.Map;

import okhttp3.Call;

import static com.funtonow.jqdk.base.UrlUtils.BAO_GUANG;
import static com.funtonow.jqdk.base.UrlUtils.CLICK_URL;
import static com.funtonow.jqdk.base.UrlUtils.EVENT_LOG;

/**
 * @author Jiajien
 * @date 2019/6/26
 */
public class StatisticsUtils {


    private static Map<String, String> result;

    /**
     * 连接点击
     */
    public static void clickUrl(GameThirdInfoListBean gameThirdInfoListBean) {
        UserInfoBean userInfoBean = SPUtil.getUserInfo();
        String httpUrl = CLICK_URL + "appid=" + Constants.OWN_APP_ID +
                "&link=" + URLEncoder.encode(gameThirdInfoListBean.getUrl()) +
                "&linkappid=" + gameThirdInfoListBean.getChanpinid() +
                "&name=" + gameThirdInfoListBean.getName() +
                "&heziid=" + gameThirdInfoListBean.getHeziId() +
                "&weizhi=" + gameThirdInfoListBean.getIndex() +
                "&gd=" + gameThirdInfoListBean.getGdId();
        LogUtil.i(httpUrl + "    clickUrl *********      ");
        result = new HashMap<>();
        if (userInfoBean != null) {
            httpUrl += "&openid=" + userInfoBean.getOpenid();
        } else {
            result.put("result", "用户信息为空" + "Android " + Build.VERSION.RELEASE
                    + PhoneUtils.getDeviceBrand() + PhoneUtils.getSystemModel());
            MobclickAgent.onEvent(SqjAppliaction.getContext(), "clickPv", result);
        }
        HttpUtil.getInstance().getDataAsynFromNet(httpUrl, new HttpUtil.MyNetCall() {
            @Override
            public void success(Call call, String jsonString) throws IOException {
                result.put("result", jsonString);
                LogUtil.i(jsonString + "  ********** clickUrl ");
                MobclickAgent.onEvent(SqjAppliaction.getContext(), "clickPv", result);
            }

            @Override
            public void failed(Call call, IOException e) {
                result.put("result", "false");
                LogUtil.i("  false  ********** clickUrl ");
                MobclickAgent.onEvent(SqjAppliaction.getContext(), "clickPv", result);
            }
        });
        Map<String, String> params = new HashMap<>();
        params.put("link", gameThirdInfoListBean.getUrl());
        params.put("linkappid", gameThirdInfoListBean.getChanpinid());
        params.put("name", gameThirdInfoListBean.getName());
        MobclickAgent.onEvent(SqjAppliaction.getContext(), "clickUrl", params);
    }

    /**
     * 产品曝光接口统计
     *
     * @param ids
     */
    public static void baoGuang(String ids) {
        String httpUrl = BAO_GUANG + "appid=" + Constants.OWN_APP_ID +
                "&heziids=" + ids;
        UserInfoBean userInfoBean = SPUtil.getUserInfo();
        if (userInfoBean != null) {
            httpUrl += "&openid=" + userInfoBean.getOpenid();
        }
        HttpUtil.getInstance().getDataAsynFromNet(httpUrl, new HttpUtil.MyNetCall() {
            @Override
            public void success(Call call, String jsonString) throws IOException {
                LogUtil.i(jsonString + "  ********** baoGuang ");
            }

            @Override
            public void failed(Call call, IOException e) {
                LogUtil.i("  failed  ********** failed ");
            }
        });
    }

    /**
     * 事件汇报接口
     *
     * @param event
     */
    public static void eventLog(String event, String channelName) {
        String httpUrl = EVENT_LOG + "appid=" + Constants.OWN_APP_ID +
                "&event=" + event +
                "&viewuuid=" + SPUtil.getUUID();
        UserInfoBean userInfoBean = SPUtil.getUserInfo();
        if (userInfoBean != null) {
            httpUrl += "&openid=" + userInfoBean.getOpenid();
        }
        switch (event) {
            case Constants.INTO_HOME:
                httpUrl += "&eventtype=" + 1;
                break;
            case Constants.INTO_LOGIN:
                httpUrl += "&eventtype=" + 2;
                break;
            case Constants.INTO_LIST:
                httpUrl += "&eventtype=" + 3;
                break;
            case Constants.GET_YZM:
                httpUrl += "&eventtype=" + 4;
                break;
            case Constants.DOWNLOAD_APP:
                httpUrl += "&eventtype=" + 5;
                break;
            case Constants.CE_BIAN_LAN:
                httpUrl += "&eventtype=" + 6;
                break;
            default:
                httpUrl += "&eventtype=" + 7;
                break;
        }
        if (event.equals(Constants.HUA_WEI_LOGO) || event.equals(Constants.DOWNLOAD_APP)) {
            httpUrl += "&detail=" + channelName;
        }
        HttpUtil.getInstance().getDataAsynFromNet(httpUrl, new HttpUtil.MyNetCall() {
            @Override
            public void success(Call call, String jsonString) throws IOException {
                LogUtil.i(jsonString + "  ********** eventLog ");
            }

            @Override
            public void failed(Call call, IOException e) {
                LogUtil.i("  failed  ********** eventLog ");
            }
        });
    }

    /**
     * 跳转到webview
     *
     * @param context   上下文
     * @param url       网址
     * @param title     标题
     * @param chanpinId 产品I
     */
    public static void intentWebView(Context context, String url, String title, String chanpinId) {
        Intent intent = new Intent(context, WebViewActivity.class);
        intent.putExtra("url", url);
        intent.putExtra("title", title);
        if (!StringUtil.isEmpty(chanpinId)) {
            intent.putExtra("chanpinId", chanpinId);
        }
        context.startActivity(intent);
    }


}
